<template>
  <el-form label-width="auto">
    <el-form-item
      v-for="item in renderList"
      :key="item.title"
    >
      <template slot="label">
        <span>
          {{ item.title }}
        </span>
        <el-tooltip :content="item.tip">
          <i class="el-icon-question"></i>
        </el-tooltip>
      </template>
      <el-select
        v-model="position[item.posIdx]"
        placeholder="请选择..."
      >
        <div v-if="item.optGroup">
          <el-option-group
            v-for="group in optionMap[item.options]"
            :key="group.label"
            :label="group.label"
          >
            <el-option
              v-for="op in group.options"
              :key="op.value"
              :label="op.label"
              :value="op.value"
            />
          </el-option-group>
        </div>
        <div v-else>
          <el-option
            v-for="op in optionMap[item.options]"
            :key="op.value"
            :label="op.label"
            :value="op.value"
          />
        </div>
      </el-select>
    </el-form-item>
    <el-form-item>
      <p>
        <span>详细技能描述请见</span>
        <router-link to="/rule">规则页</router-link>
      </p>
      <el-button @click="onSubmit" type="primary">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import skillConf from '@/config/skillConf.json';
const ROW_MAX = 9;
const LINE_MAX = 8;
const skill = ['s1', 's2', 's3', 's4', 's5', 's6'];
export default {
  name: 'Setting',
  data() {
    return {
      renderList: skillConf.renderList,
      optionMap: skillConf.optionMap,
      position: [null, null, null, null, null, null]
    };
  },
  methods: {
    onSubmit() {
      // 验证同一棋子不能赋予多个技能
      if (!this.position.every(e => e) || new Set(this.position).size !== this.position.length) {
        this.$message({
          type: 'warning',
          message: '注意：技能不能为空，同一棋子不能赋予多个技能'
        });
        return;
      }
      const result = skill.reduce((pre, cur, idx) => ({
        ...pre,
        [cur]: this.position[idx]
      }), {});
      Object.keys(result).forEach(key => {
        const temp = result[key];
        result[key] = this.$cookies.get('room').side === 0
          ? [ROW_MAX - parseInt(temp / 10, 10), LINE_MAX - temp % 10]
          : [parseInt(temp / 10, 10), temp % 10];
      });
      this.$emit('onSubmit', result);
    }
  }
};
</script>
